package com.ahua.mallchat.common.adapter;

import com.ahua.mallchat.common.domain.enums.system.YesOrNoEnum;
import com.ahua.mallchat.common.domain.enums.ws.WSRespTypeEnum;
import com.ahua.mallchat.common.domain.pojo.User;
import com.ahua.mallchat.common.domain.vo.WSBaseResp;
import com.ahua.mallchat.common.domain.vo.ws.WSBlack;
import com.ahua.mallchat.common.domain.vo.ws.WSLoginSuccess;
import com.ahua.mallchat.common.domain.vo.ws.WSLoginUrl;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;

public class WebSocketAdapter {

    /**
     * 构建登陆二维码的消息返回体
     *
     * @param wxMpQrCodeTicket
     * @return
     */
    public static WSBaseResp<?> buildResp(WxMpQrCodeTicket wxMpQrCodeTicket) {
        WSBaseResp<WSLoginUrl> resp = new WSBaseResp<>();
        WSLoginUrl wsLoginUrl = new WSLoginUrl();
        wsLoginUrl.setLoginUrl(wxMpQrCodeTicket.getUrl());
        resp.setType(WSRespTypeEnum.LOGIN_URL.getType());
        resp.setDate(wsLoginUrl);
        return resp;
    }

    public static WSBaseResp<?> buildLoginSuccessResp(User user, String token, boolean hasPower) {
        WSBaseResp<WSLoginSuccess> resp = new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.LOGIN_SUCCESS.getType());

        WSLoginSuccess loginSuccess = new WSLoginSuccess();
        loginSuccess.setAvatar(user.getAvatar());
        loginSuccess.setUid(user.getId());
        loginSuccess.setName(user.getName());
        loginSuccess.setToken(token);
        loginSuccess.setPower(hasPower ? YesOrNoEnum.YES.getStatus() : YesOrNoEnum.NO.getStatus());
        resp.setDate(loginSuccess);

        return resp;
    }

    public static WSBaseResp<?> buildWaitAuthorizeResp() {
        WSBaseResp resp = new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.LOGIN_SCAN_SUCCESS.getType());
        return resp;
    }

    public static WSBaseResp<?> buildInvalidateTokenResp() {
        WSBaseResp resp = new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.INVALIDATE_TOKEN.getType());
        return resp;
    }

    public static WSBaseResp<?> buildBlack(User user) {
        WSBaseResp<WSBlack> resp = new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.BLACK.getType());

        WSBlack wsBlack = new WSBlack();
        wsBlack.setUid(user.getId());

        resp.setDate(wsBlack);
        return resp;
    }
}
